package com.hkzhao.shixun.component;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * <h3>j2ee</h3>
 * <p>redis aop操作</p>
 *防止redis没了影响业务逻辑  目前没什么用 权衡自己写template和spring cache的问题 现在还不使用
 * @author : hkzhao
 * @date : 2020-06-05 22:42
 **/
@Aspect
@Component
@Order(2)
public class RedisCacheAspect {
    private static Logger LOGGER = LoggerFactory.getLogger(RedisCacheAspect.class);
    @Pointcut("execution(public * com.hkzhao.shixun.service.*CacheService.*(..))")
    public void cacheAspect(){

    }

    @Around("cacheAspect()")
    public Object doAround(ProceedingJoinPoint joinPoint)throws Throwable{
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature)signature;
        Method method = methodSignature.getMethod();
        Object result =null;
        try{
            result = joinPoint.proceed();
        }catch (Throwable throwable){
            //有CacheException注解的方法需要抛出异常
            if(method.isAnnotationPresent(CacheException.class)){
                throw throwable;
            }else {
                LOGGER.error(throwable.getMessage());
            }
        }
        return result;
    }
}
